﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.Utility;

namespace EHTSaleManager.UIAdmin
{
    public partial class AdminLevelService : System.Web.UI.Page
    {
        public Int32 SeviceLevelID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["SeviceLevelID"] != null)
                {
                    try
                    {
                        SeviceLevelID = Int32.Parse(Request.QueryString["SeviceLevelID"].ToString());
                    }
                    catch (Exception ex)
                    {
                        SeviceLevelID = -1;
                    }
                }

                if (SeviceLevelID == -1)
                {
                    ViewState["action"] = HangSo.ACTION_ADD;

                }
                else
                {
                    ServiceLevels theServiceLevel = ServiceLevelsProvider.GetServiceLevelByID(SeviceLevelID);

                    if (theServiceLevel == null)
                        Response.Redirect("AdminLevelServices.aspx");
                    else
                    {
                        this.txtServiceName.Text = theServiceLevel.Description;                        
                        this.txtServiceCost.Text = theServiceLevel.ServiceCost.ToString().Trim();
                                              
                        ViewState["ID"] = theServiceLevel.SeviceLevelID;
                        ViewState["action"] = HangSo.ACTION_UPDATE;
                    }
                }
            }
        }


        private Boolean ValidateData()
        {
            String message = "";
            Boolean isSuccess = true;

            try
            {
                float value = float.Parse(this.txtServiceCost.Text.Trim());
            }
            catch (Exception ex)
            {
                isSuccess = false;
                message += "Tjenesten Cost må er nummer";
            }

            if (!isSuccess)
                ltrMessage.Text = message;

            return isSuccess;
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminLevelServices.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();

            if (!ValidateData())
                return;

            ServiceLevels theService = new ServiceLevels();
          
            theService.Description = this.txtServiceName.Text.Trim();                    
            try
            {
                theService.ServiceCost = float.Parse(this.txtServiceCost.Text.Trim());
            }
            catch (Exception ex)
            {
                theService.ServiceCost = 0;
            }

            if (action == HangSo.ACTION_ADD)
            {
                theService.SeviceLevelID = -1;
                ServiceLevelsProvider.IUServiceLevels(theService,true);
            }
            else
            {
                theService.SeviceLevelID = Int32.Parse(ViewState["ID"].ToString());
                ServiceLevelsProvider.IUServiceLevels(theService,false);
            }

            Response.Redirect("AdminLevelServices.aspx");
        }
        
    }
}